<?php
class Admin_Form_Member extends Zend_Form
{
    public function init()
    {
        $this->setName('formMember');

        $this->addElement('text', 'email', array(
            'label'      => 'email',
            'class'      => 'span6',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress')
        ));

        $this->addElement('text', 'firstname', array(
            'label'        => 'firstname',
            'filters'      => array('StringTrim'),
            'class'        => 'span6',
            'validators'   => array(
                'Alpha',
                array('StringLength', false, array(
                    'min' => Ext_App::getSetting('length.username.min'),
                    'max' => Ext_App::getSetting('length.username.max')
                    )
                )
            )
        ));

        $this->addElement('text', 'lastname', array(
            'label'        => 'lastname',
            'filters'      => array('StringTrim'),
            'class'        => 'span6',
            'validators'   => array(
                'Alpha',
                array('StringLength', false, array(
                    'min' => Ext_App::getSetting('length.username.min'),
                    'max' => Ext_App::getSetting('length.username.max')
                    )
                )
            )
        ));

        $this->addElement('text', 'username', array(
            'label'        => 'username',
            'required'     => true,
            'filters'      => array('StringTrim'),
            'class'        => 'span6',
            'validators'   => array(
                'NotEmpty',
                'Alnum',
                array('StringLength', false, array(
                    'min' => Ext_App::getSetting('length.username.min'),
                    'max' => Ext_App::getSetting('length.username.max')
                    )
                )
            )
        ));

        $this->addElement('select', 'gender', array(
            'label'        => 'gender',
            'multiOptions' => Model_Member::$gender,
            'class'        => 'span3'
        ));

        $this->addElement('select', 'role', array(
            'label'        => 'role',
            'multiOptions' => Model_Member::$roles,
            'class'        => 'span3'
        ));

        $this->addElement('select', 'status', array(
            'label'        => 'status',
            'multiOptions' => Model_Member::$statuses,
            'class'        => 'span3'
        ));

        $this->addElement('submit', 'register', array('class' => 'btn'));
    }

    public function save()
    {
        $data = array_merge(array(
            'role'       => Model_Member::ROLE_MEMBER,
            'status'     => Model_Member::STATUS_NOT_VERIFIED,
            'verify_key' => Ext_App::getToken(Ext_App::getSetting('token.verifyKey.salt'))
        ), $this->getValues());
        $newMember = Ext_App::getTable('Model_DbTable_Member')->createRow($data);
        $newMember->save();
        return $newMember;
    }
}